{extend name="../../base/view/common/base" /}
<!-- 主体 -->
{block name="body"}
<div class="p-3">
    <form class="layui-form gg-form-bar border-x border-t">
        <div class="layui-input-inline" style="width:300px;">
            <input type="text" name="name" placeholder="物料名称" class="layui-input" autocomplete="off" />
        </div>
        <div class="layui-input-inline">
            {if condition="$eid gt 0"}
            <input type="hidden" name="eid" value="{$eid}" lay-verify="required">
            {else/}
            <select name="eid" lay-filter="status">
                <option value="0">系统管理员</option>
                {volist name="enterprise" id="v"}
                <option value="{$v.id}">{$v.title}</option>
                {/volist}
            </select>
            {/if}
        </div>
        <div class="layui-input-inline" style="width:150px;">
            <button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="webform"><i class="layui-icon layui-icon-search mr-1"></i>搜索</button>
            <button type="reset" class="layui-btn layui-btn-reset" lay-filter="reset">清空</button>
        </div>
    </form>
    <table class="layui-hide" id="test" lay-filter="test"></table>
</div>

<script type="text/html" id="toolbarDemo">
  <div class="layui-btn-container">
    <span class="layui-btn layui-btn-sm" title="添加客户" lay-event="add">+ 领用物料</span>
  </div>
</script>

{/block}
<!-- /主体 -->

<!-- 脚本 -->
{block name="script"}
	<script>
	const moduleInit = ['tool'];

    //表头工具栏事件

	function gouguInit() {
		var table = layui.table, tool = layui.tool, form = layui.form;

        table.on('toolbar(test)', function(obj){
            if (obj.event === 'add') {
                tool.side("/home/goodsCollar/add");
                return;
            }
        });

        table.on('tool(test)',function (obj) {
            if (obj.event === 'del') {
                layer.confirm('确定要删除吗？', { icon: 3, title: '提示' }, function (index) {
                    let callback = function (e) {
                        layer.msg(e.msg);
                        if (e.code == 0) {
                            layui.pageTable.reload();
                        }
                    }
                    tool.post("/home/goodsCollar/delete", { id: obj.data.id }, callback);
                    layer.close(index);
                });
            }
        });


        layui.pageTable = table.render({
			elem: '#test'
			,toolbar: '#toolbarDemo'
			,defaultToolbar: false
			,title:'物料列表'
			,url: "/home/goodsCollar/index"
			,page: true
			,cellMinWidth: 80
			,cols: [[
                    {field:'id',width:80, title: 'ID号', align:'center'}
                    ,{field:'d_name', title: '所属企业' }
                    ,{field:'out_code',title: '出库编号'}
                    ,{field:'use_user',title: '领用人'}
					,{field:'out_code',title: '出库编号'}
					,{field:'fmt_out_time',title: '出库时间'}
					,{field:'item_name',title: '项目名称'}
					,{field:'num',title: '领用数量'}
					,{field:'money',title: '总金额(元)'}
					,{width:150,title: '操作', align:'center',templet: function(d){
						var html='';

                        var btn='<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="look">详情</a>';
                        html = '<div class="layui-btn-group">'+btn+'<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>'+'</div>';
						return html;
					}}
				]],
                done: function (res) {
                    // console.log(res,"{$eid}")
                    // if ("{$eid}" != 0) {
                    //     $("[data-field='d_name']").css('display', 'none');
                    // }

                    return res;
                }
			});
			
			table.on('tool(test)',function (obj) {
				if(obj.event === 'look'){
                    tool.side('/home/goodsCollar/info?id='+obj.data.id);
                    return;
				}
                if (obj.event === 'del') {
                    layer.confirm('确定要删除吗？', { icon: 3, title: '提示' }, function (index) {
                        let callback = function (e) {
                            layer.msg(e.msg);
                            if (e.code == 0) {
                                layui.pageTable.reload();
                            }
                        }
                        tool.post("/home/goodsCollar/delete", { id: obj.data.id }, callback);
                        layer.close(index);
                    });
                }
			});
			
			$('body').on('click','.addNew',function(){
				addExpense(0,'');	
			});
			
			function addExpense(id,val){
				var title = '新增物料';
				if(id>0){
					title = '编辑物料';
				}
				layer.prompt({
					title: title,
					value: val,
					yes: function(index, layero) {
						// 获取文本框输入的值
						var value = layero.find(".layui-layer-input").val();
						if (value) {
							let callback = function (e) {
								layer.msg(e.msg);
								if (e.code == 0) {
									layui.pageTable.reload();				
								}
							}
							tool.post("/home/goodsCollar/add", {id: id,title: value}, callback);
							layer.close(index);
						} else {
							layer.msg('请填写物料名称');
						}
					}
				})
			}


        //监听搜索提交
        form.on('submit(webform)', function (data) {
            layui.pageTable.reload({ where: data.field });
            return false;
        });
		}
	</script>
{/block}
<!-- /脚本 -->